package com.game.play;

import java.util.ArrayList;
import java.util.List;

import com.game.cards.BaseCard;
import com.game.cards.Deck;
import com.game.cards.Flags;
import com.game.cards.MonsterCard;
import com.game.cards.decks.Adascar;
import com.game.cards.decks.BurnDeck;
import com.game.cards.decks.StarterDeck;
import com.game.cards.decks.WarriorDeck;

public class Game {
	
	private Player player1, player2;
	private Player currentTurn;
	private int totalTurns;
	private List<Deck> decks;
	private GamePhase phase;
	private boolean isPlaying;
	public Game(Player p1, Player p2)
	{
		this.player1 = p1;
		this.player2 = p2;
		this.currentTurn = null;
		this.decks = new ArrayList<Deck>();
		this.decks.add(new StarterDeck(this));
		this.decks.add(new Adascar(this));
		this.decks.add(new BurnDeck(this));
		this.decks.add(new WarriorDeck(this));
		this.isPlaying = false;
		
		this.totalTurns = 0;
		this.setPhase(null);
	}
	public void CoinToss()
	{
		java.util.Random rand = new java.util.Random();
		int toss = rand.nextInt(2);
		if(toss == 0) {
			this.setCurrentTurn(player1);
			System.out.println(player1.getName() + " will go first!");
		}
		else {
			this.setCurrentTurn(player2);
			System.out.println(player2.getName() + " will go first!");
		}
		
	}
	public List<Deck> getDecks() {
		return decks;
	}
	public void setDecks(List<Deck> decks) {
		this.decks = decks;
	}

	public BaseCard getCardById(int id)
	{
		for (BaseCard b :  BaseCard.getAllCardsEver())
		{
			if (b.getId() == id) return b;
		}
		return null;
	}
	public Player getPlayerByName(String name)
	{
		if (player1.getName().equals(name)) return player1;
		if (player2.getName().equals(name)) return player2;
		return null;
	}
	public Player checkWin()
	{
		if (this.player1.getLifePoints() <= 0) return this.player2;
		if (this.player2.getLifePoints() <= 0) return this.player1;
		
		if (this.player1.getCurrentDeck().deckSize() <= 0) return this.player2;
		if (this.player2.getCurrentDeck().deckSize() <= 0) return this.player1;
		
		return null;
	}
	public void resetMonsterAttacks()
	{
		for (BaseCard c : BaseCard.getAllCardsEver()){
			if (c instanceof MonsterCard)
			{
				((MonsterCard) c).setAttacks(((MonsterCard) c).getAttacksPerTurn());
				
			}
		}
	}
	public void attack(MonsterCard f, Player p)
	{
		p.setLifePoints(p.getLifePoints()-f.getAttack());
		System.out.println(f.getTitle() + " attacked " + p.getName() + " directly for " + f.getAttack() + "!");
		
	}
	public void attack(MonsterCard f, MonsterCard t)
	{
		f.getDeck().getOwner().getAq().addEvent(Events.attackmonster + "," + f.getId() + "," + t.getId());
		if (f.getAttack() >= t.getDefense() && !t.getFlags().contains(Flags.invincible)) {
			if (f.getFlags().contains(Flags.piercing)) {
				t.getDeck().getOwner().setLifePoints(t.getDeck().getOwner().getLifePoints() - (f.getAttack() - t.getDefense()));
				System.out.println(f.getTitle() + " pierced through to deal " + (f.getAttack() - t.getDefense()) + " damage!");
			}
			if(t.getDeck().getOwner().getField().removeMonsterById(t.getId())) {
				t.getDeck().getOwner().getField().getGraveyard().add(t);
			}
			System.out.println(f.getTitle() + " attacked and destroyed " + t.getTitle() + "!");
			f.getDeck().getOwner().getAq().addEvent(Events.destroycard +"," +t.getId());
		}
		else if (f.getAttack() <= t.getDefense())
		{
			f.getDeck().getOwner().setLifePoints(f.getDeck().getOwner().getLifePoints() - (t.getDefense()-f.getAttack()));
			System.out.println(f.getTitle() + " attacked " + t.getTitle() + " and hurt it's player for " + (t.getDefense()-f.getAttack()));
			
		}
	}
	public void nextTurn()
	{
		
		
		this.checkAQ();
		this.currentTurn.getAq().addEvent(Events.endturn.toString() +"," +currentTurn.getName());
		Player test = checkWin();
		if (test != null) {
			this.isPlaying = false;
			System.out.println("Game over!");
			System.out.println(test.getName() + " wins!");
		}
		this.currentTurn = (this.currentTurn.equals(this.player1)? this.player2 : this.player1);
			this.totalTurns++;
			this.resetMonsterAttacks();
			
			
			
	}
	public void checkAQ()
	{
		List<MonsterCard> del = new ArrayList<MonsterCard>();
		for (MonsterCard mc : player1.getAq().getEffects())
		{
			if (!isOnField(mc)) del.add(mc);
		}
		player1.getAq().getEvents().removeAll(del);
	}
	public boolean isOnField(MonsterCard c)
	{
		if (this.player1.getField().getMonsterCards().contains(c)) return true;
		if (this.player2.getField().getMonsterCards().contains(c)) return true;
		return false;
	}
	public Player getPlayer1() {
		return player1;
	}
	public void setPlayer1(Player player1) {
		this.player1 = player1;
	}
	public Player getCurrentTurn() {
		return currentTurn;
	}
	public Player getOffTurn()
	{
		return (currentTurn == player1? player2 : player1);
	}
	public void setCurrentTurn(Player currentTurn) {
		this.currentTurn = currentTurn;
	}
	public Player getPlayer2() {
		return player2;
	}
	public void setPlayer2(Player player2) {
		this.player2 = player2;
	}
	public int getTotalTurns() {
		return totalTurns;
	}
	public void setTotalTurns(int totalTurns) {
		this.totalTurns = totalTurns;
	}

	public GamePhase getPhase() {
		return phase;
	}

	public void setPhase(GamePhase phase) {
		this.phase = phase;
	}
	public boolean isPlaying() {
		return isPlaying;
	}
	public void setPlaying(boolean isPlaying) {
		this.isPlaying = isPlaying;
	}
	
	

}
